package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.pojo.entity.User;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;

/**
 * 普通用户 controller 层
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        this.userService.add(user);
        return Result.success();
    }

    @GetMapping("/pageQuery")
    public Result pageQuery(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            User user
    ) {
        PageResult<User> pageResult = this.userService.pageQuery(pageNum, pageSize, user);
        return Result.success(pageResult);
    }

    @GetMapping("/query")
    public Result queryById(@RequestParam Integer id) {
        User user = this.userService.queryById(id);
        return Result.success(user);
    }

    @GetMapping("/query/all")
    public Result queryAll() {
        List<User> userList = this.userService.queryAll();
        return Result.success(userList);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        this.userService.update(user);
        return Result.success();
    }

    @PutMapping("/recharge/{amount}")
    public Result rechargeAccount(@PathVariable BigDecimal amount) {
        this.userService.rechargeAccount(amount);
        return Result.success();
    }
}
